package com.suli.multimedia.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.scenic.util.ResponseResult;
import com.suli.multimedia.entity.ScenicTicket;
import com.suli.multimedia.entity.User;
import com.suli.multimedia.entity.UserTicket;
import com.suli.multimedia.service.ScenicTicketService;
import com.suli.multimedia.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author SuLi
 * @since 2022-04-02
 */
@RestController
@CrossOrigin("*")
@RequestMapping("/ticket")
public class ScenicTicketController {

    @Autowired
    ScenicTicketService ticketService;

    @Autowired
    UserService userService;

    /**
     * 添加门票
     * @param ticket 门票信息
     * @return 添加结果
     */
    @PostMapping("/addTicket")
    public ResponseResult addTicket(@RequestBody ScenicTicket ticket){
        if(ticketService.save(ticket)){
            return ResponseResult.ok().message("发布成功");
        }
        return ResponseResult.error().message("发布失败");
    }

    /**
     * 获取门票列表
     * @param limit 数量
     * @param page 页码
     * @return 门票列表
     */
    @GetMapping("/getTicketList/{page}/{limit}")
    public ResponseResult getTicketList(@PathVariable Long limit, @PathVariable Long page){
        Map<String, Object> ticketByPage = ticketService.getTicketByPage(page, limit);
        if(!ObjectUtils.isEmpty(ticketByPage))
        return ResponseResult.ok().data(ticketByPage);
        return ResponseResult.error().message("获取列表失败");
    }

    /**
     * 用户获取门票列表
     * @param limit 数量
     * @param page 页码
     * @return 门票列表
     */
    @GetMapping("/getTicketListUser/{page}/{limit}")
    public ResponseResult getTicketListUser(@PathVariable Long limit, @PathVariable Long page){
        Map<String, Object> ticketByPage = ticketService.getTicketByPageUser(page, limit);
        if(!ObjectUtils.isEmpty(ticketByPage))
            return ResponseResult.ok().data(ticketByPage);
        return ResponseResult.error().message("获取列表失败");
    }

    /**
     * 根据门票id获取门票
     * @param id 门票id
     * @return 门票
     */
    @GetMapping("/getTicket/{id}")
    public ResponseResult getTicket(@PathVariable Integer id){
        ScenicTicket scenicTicket = ticketService.getById(id);
        if(!ObjectUtils.isEmpty(scenicTicket))
            return ResponseResult.ok().data("ticket",scenicTicket);
        return ResponseResult.error().message("获取列表失败");
    }

    /**
     * 根据id删除门票
     * @param ticketIds 门票id
     * @return 删除结果
     */
    @DeleteMapping("/deleteTicket")
    public ResponseResult deleteTicket(@RequestBody List<String> ticketIds){
        if(ticketService.deleteTicketByIds(ticketIds)){
            return ResponseResult.ok().message("删除成功");
        }
        return ResponseResult.error().message("删除失败");
    }

    /**
     * 根据email或者name搜索门票
     * @param eOrN email或者name
     * @return 门票信息
     */
    @GetMapping("/searchTicket/{eOrN}/{page}/{size}")
    public ResponseResult searchTicket(@PathVariable String eOrN,
                                       @PathVariable Long page,
                                       @PathVariable Long size){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        Map<String, Object> map;
        if(Pattern.matches("^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^.-]+@[a-zA-Z0-9.-]+$", eOrN)){
            queryWrapper.eq("user_email",eOrN);
            User one = userService.getOne(queryWrapper);
            if(ObjectUtils.isEmpty(one)){
                return ResponseResult.error().message("查询失败,请确认用户名或邮箱地址是否正确");
            }
            map = ticketService.searchTicketByEmail(eOrN,page,size);
        }else {
            queryWrapper.eq("user_name",eOrN);
            User one = userService.getOne(queryWrapper);
            if(ObjectUtils.isEmpty(one)){
                return ResponseResult.error().message("查询失败,请确认用户名或邮箱地址是否正确");
            }
            map = ticketService.searchTicketByName(eOrN,page,size);
        }
        return ResponseResult.ok().data(map);
    }

    /**
     * 根据订单id查询门票
     * @param id 订单id
     * @return 门票
     */
    @GetMapping("/searchTicketOne/{id}")
    public ResponseResult searchTicketOne(@PathVariable String id){
        Map<String, Object> map = ticketService.searchTicketById(id);
        if(!ObjectUtils.isEmpty(map.get("userTicket")))
        return ResponseResult.ok().data(map);
        return ResponseResult.error().message("查询失败，请确认输入订单号正确");
    }

    /**
     * 创建订单
     * @param ticket 门票信息
     * @return 创建结果
     */
    @PostMapping("/createOrder")
    public ResponseResult createOrder(@RequestBody UserTicket ticket){
        System.out.println(ticket);
        Map<String, String> order = ticketService.createOrder(ticket);
        if(order.get("message").equals("门票购买成功"))
        return ResponseResult.ok().message(order.get("message"));
        return ResponseResult.error().message(order.get("message"));
    }
}

